package upload;

import utils.StreamUtils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.Socket;

/**
 * @author xingzhiwen
 * @date 2021/9/14
 * @ddescription:
 */
public class TCPFileUploadClient {
    public static void main(String[] args) throws Exception {
        //客户端连接服务端
        Socket socket = new Socket("127.0.0.1", 8888);
        //创建读取磁盘文件的输入流
        String filePath = "E:\\ZhejiangSci-TechUniversity\\UltravioletProject\\test\\picture\\1.jpg";
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
        //bytes就是文件对应的字节数组
        byte[] bytes = StreamUtils.streamToByteArray(bis);
        //铜鼓socket获取到输出流，将bytes数据发送给服务端
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        bos.write(bytes);//将文件写入数据通道
        bis.close();
        socket.shutdownOutput();//设置了写入数据结束标记

        //接受从服务端回复的消息，字节流
        InputStream inputStream = socket.getInputStream();

        //使用方法，将读取到的内容转成字符串
        String string = StreamUtils.streamToString(inputStream);
        System.out.println(string);


        bos.close();
        socket.close();

    }
}
